package com.tamecode.listener;

import org.flowable.engine.common.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.common.api.delegate.event.FlowableEvent;
import org.flowable.engine.common.api.delegate.event.FlowableEventListener;
import org.flowable.idm.api.event.FlowableIdmEventType;

import static org.flowable.engine.common.api.delegate.event.FlowableEngineEventType.JOB_EXECUTION_FAILURE;
import static org.flowable.engine.common.api.delegate.event.FlowableEngineEventType.JOB_EXECUTION_SUCCESS;

public class MyEventListener implements FlowableEventListener {

  @Override
  public void onEvent(FlowableEvent event) {

    FlowableEngineEventType type = (FlowableEngineEventType) event.getType();

    System.out.println(type.name());

    switch (type) {

      case JOB_EXECUTION_SUCCESS:
        System.out.println("A job well done!");
        break;

      case JOB_EXECUTION_FAILURE:
        System.out.println("A job has failed...");
        break;

      default:
        System.out.println("Event received: " + type);
    }
  }

  @Override
  public boolean isFailOnException() {
    // onEvent方法中的逻辑并不重要，可以忽略日志失败异常……
    // isFailOnException()方法决定了当事件分发后，onEvent(..)方法抛出异常时的行为。
    // 若返回false，忽略异常；若返回true，异常不会被忽略而会被上抛。若抛出，事务会被回滚，会影响业务操作。不重要建议返回false.

    return false;
  }

  @Override
  public boolean isFireOnTransactionLifecycleEvent() {
    return false;
  }

  @Override
  public String getOnTransaction() {
    return null;
  }
}